import pino from 'pino';
import { createLogger, format, transports } from 'winston';
import fs from 'fs';
import path from 'path';

const transport = pino.transport({
  target: 'pino-pretty',
});

export const logger = pino(
  {
    level: 'info',
    redact: ['poolKeys'],
    serializers: {
      error: pino.stdSerializers.err,
    },
    base: undefined,
  },
  transport,
);

const poolSizeLogPath = path.join(process.cwd(), 'logs', 'pool-sizes.json');

// 确保日志目录存在
if (!fs.existsSync(path.dirname(poolSizeLogPath))) {
  fs.mkdirSync(path.dirname(poolSizeLogPath), { recursive: true });
}

// 初始化日志文件
if (!fs.existsSync(poolSizeLogPath)) {
  fs.writeFileSync(poolSizeLogPath, JSON.stringify([], null, 2));
}

export const logPoolSize = (data: { formatted: string; mint: string }) => {
  try {
    const existingData = JSON.parse(fs.readFileSync(poolSizeLogPath, 'utf-8'));
    existingData.push({
      ...data,
      timestamp: new Date().toISOString(),
    });
    fs.writeFileSync(poolSizeLogPath, JSON.stringify(existingData, null, 2));
  } catch (error) {
    logger.error('写入池子大小日志失败:', error);
  }
};
